#include<iostream>
#include<assert.h>
#include<string.h>

using namespace std;

namespace MyVector{
  template<class T>
  class vector{
    
  public:
    /*定义迭代器类型*/
  typedef T* iterator;
  typedef const T* const_iterator;
  
  /*返回迭代器*/
  iterator begin(){
    return _start;
  }

  iterator end(){
    return _finish;
  }

  const_iterator begin()const{
    return _start;
  }

  const_iterator end()const{
    return _finish;
  }

  /*返回个参数函数*/
  //size
  size_t size(){
    return _finish-_start;
  }
  
  size_t capacity(){
    return _end_of_storage - _start;
  }
  /*构造函数*/

  //默认构造函数
  vector()
  :_start(nullptr)
   ,_finish(nullptr)
   ,_end_of_storage(nullptr)
  {}

  //传值构造函数
  explicit vector(size_t n,const T&val = T())
  :_start(nullptr)
   ,_finish(nullptr)
   ,_end_of_storage(nullptr)
  {
    T* tmp = new T[n];
    _start = tmp;
    for(size_t i = 0;i<n;++i){
      push_back(val);
    }

  }

  /*析构函数*/
  ~vector(){
    delete  []_start;
    _start = _finish = _end_of_storage = nullptr;
  }
  
  //拷贝构造
  vector(const vector<T>&v){
    T* tmp = new T[v.size()];
    _start = tmp;
    memcpy(tmp,v._start,sizeof(T)*v.size());
    _finish = _end_of_storage = _start + v.size();
  }

  /*扩容reserve*/
  void reserve(size_t n){
    if(n>capacity()){
      size_t count = size();
      T* tmp = new T[n];
      memcpy(tmp,_start,sizeof(T)*count);
      delete[]_start;
      _start = tmp;
      _finish = _start+count;
      _end_of_storage = _start+n;
    }
   }

  /*插入函数*/
  //单个插入
  iterator insert(iterator position,const T&val){
    assert(position<=_finish);
    assert(position>=_start);
    size_t count = position - begin();//记录偏移量
    if(_end_of_storage==_finish){//需要扩容
     size_t n = _start==nullptr?4:1.5*capacity();
     reserve(n);
     position = begin()+count;//还原原始pos位置
    }
    iterator begin = end();
    while(begin!=position){
      *begin = *(begin-1);
      begin--;
    }
    *begin = val;
    _finish++;
    return position;
  }
  
  void insert(iterator position,size_t n,const T&val){
    assert(position>=_start);
    assert(position<=_finish);
    size_t count = position-_start;
    if(size()+n>=capacity()){
      reserve(capacity()+n);
      position = _start+count;
    }
    iterator begin = end()+n-1;
    while(begin!=position){
      *begin = *(begin-n);
      --begin;
    }
    for(size_t i =0;i<n;++i){
      *(begin+i) = val;
    }
    _finish+=n;
  }


  //尾插
  void push_back(const T&val){
    insert(end(),val);
  }

  //清除函数
  void clear(){
    *_start = 0;
    _finish = _start;
  }

  //交换函数
  void swap( vector<T>&v){
    ::swap(_start,v._start);
    ::swap(_finish,v._finish);
    ::swap(_end_of_storage,v._end_of_storage);
  }

  //赋值重载
  vector<T>& operator=(vector<T>tmp){
    swap(tmp);//传值 这里由于tmp作为参数将会进行拷贝构造
              //拷贝构造后即生成临时对象,临时对象再与对象进行交换即完成赋值重载
    return *this;
  }

  //清除某个位置的数据
  iterator erase(iterator position){
    assert(position>=_start&&position<_finish);

    iterator it = position+1;
    while(it<_finish){
      *(it-1) = *it;
      ++it;
    }
    --_finish;
    return position;
  }
 
  void resize(size_t n,T val = T()){
    if(n<size()){
      _finish = _start+n;
    }
    else{
      insert(end(),n-size(),val);
    }

  }

  // []重载
  T& operator[](size_t n){
    assert(n<size());
    return *(_start+n);
  }

 const T& operator[](size_t n)const{
    assert(n<size());
    return *(_start+n);
  }

 /*头尾函数*/
   T& front(){
    return _start[0];
  }

  const T& front()const{
    return _start[0];
  }
   T& back(){
    return _start[size()-1];
  }

  const T& back()const{
    return _start[size()-1];
  }
  private:
    iterator _start;
    iterator _finish;
    iterator _end_of_storage;
  };


}
